package com.mima.minaserver.service;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mima.minaserver.client.AliClient;
import com.mima.minaserver.repository.IotServerMessageManagerRepository;

@Component
public class AliCloudMessage {

	private static Logger logger = LoggerFactory.getLogger(AliCloudMessage.class);

	@Autowired
	private IotServerMessageManagerRepository iotServerMessageManagerRepository;

	// 推送设备账号信息
	public void PushLoginInfo(String uuid,int keyId,String secretkey, String url) throws ClientProtocolException, IOException {
		try {
			Object result = iotServerMessageManagerRepository.deviceLoginInfo(uuid);

			if (result == null) {
				logger.info("{UUID=" + uuid + ", NOT FOUND!}");
				return;
			}
			Object[] objects = (Object[]) result;
			logger.info("{UUID=" + uuid + ",URL=" + url + ",ACCOUNT=" + (String) objects[0] + ",PASSWORD="
					+ (String) objects[1] + ",SECRET_KEY=" + secretkey + ",keyId="+keyId+"}");
			int ali_response_status = AliClient.sendPostDataByJson(url, uuid, (String) objects[0], (String) objects[1],secretkey,keyId);
			System.out.println(ali_response_status);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
